package com.cb.controller.member.order;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.common.util.ConfigUtil;
import com.cb.common.webapp.context.RequestContext;
import com.cb.core.command.QueryFilter;
import com.cb.core.command.QueryResult;
import com.cb.core.controller.BaseListController;
import com.cb.domain.model.order.BOrder;
import com.cb.domain.model.user.UserAccount;
import com.cb.domain.service.order.BOrderManager;
import com.cb.domain.service.order.OrderLogManager;
import com.cb.domain.service.order.PassengerManager;

/**
 * 
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 */
public class OrderController extends BaseListController {
	
	@Autowired
	private BOrderManager bOrderManager;
	@Autowired
	private PassengerManager passengerManager;
	@Autowired
	private OrderLogManager orderLogManager;

	
	/**对象列表页，提供最基本的分页显示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		UserAccount account=RequestContext.getCurrentUser();
		QueryFilter filter=new QueryFilter(request);
		if(account!=null){
			filter.addFilter("Q_userAccount.userId_L_EQ", account.getUserId().toString());
		}else{
			filter.addFilter("Q_userAccount.userId_L_EQ", "-1");
		}
		filter.addSorted("createTime", QueryFilter.SORT_DESC);
		QueryResult queryResult=bOrderManager.getAll(filter);
		System.out.println("");
		
		return new ModelAndView("member/order/orderList","bOrders",queryResult);
	}
	
	/**
	 * 未登录查询订单
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView logoutOrders(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QueryFilter filter=new QueryFilter(request);
		HttpSession session=request.getSession();
		String valiCode = request.getParameter("valiCode");
		if(valiCode!=null){
			String vcode = session.getAttribute("vcode").toString();
			if(!valiCode.toUpperCase().equals(vcode.toUpperCase())){
				request.getSession().setAttribute("msg", "验证码错误!");
				return new ModelAndView(getCancelView());
			} else{
				request.getSession().removeAttribute("msg");
			}
		}

		QueryResult queryResult=bOrderManager.getAll(filter);
		
		return new ModelAndView("member/order/orderList","bOrders",queryResult);
	}
	
	/**
	 * UPDATE状态
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView updateStatus(HttpServletRequest request,HttpServletResponse response){
		String orderId=request.getParameter("id");
		String status=request.getParameter("status");
		if(StringUtils.isNotEmpty(orderId)&&StringUtils.isNotEmpty(status)){
			BOrder order=bOrderManager.get(new Long(orderId));
			if(null!=order){
				order.setStatus(new Long(status));
				bOrderManager.save(order);
			}
		}
		
		return new ModelAndView(getSuccessView());
	}
	
	
	/**
	 * 拆分订单
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView splitOrderObj(HttpServletRequest request,HttpServletResponse response){
		String orderId=request.getParameter("id");
		if(StringUtils.isNotEmpty(orderId)){
			BOrder order=bOrderManager.get(new Long(orderId));
			if(null!=order){
				String res=bOrderManager.splitBOrder(order);
				saveMessage(request, res);
			}
		}
		return new ModelAndView(getSuccessView());
	}
	
	
	
	/**
	 * 批量删除，通过页面传回对象的主键数组进行逐条删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids=request.getParameterValues("id");
		if(ids!=null){
			for(String id:ids){
				bOrderManager.remove(new Long(id));
			}
			saveMessage(request, getText("bOrder.mutiDeleted"));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 删除单个实体对象
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request,	HttpServletResponse response){
		String id=request.getParameter("id");
		if(StringUtils.isNotEmpty(id)){
			BOrder bOrder=bOrderManager.get(new Long(id));
			bOrderManager.remove(bOrder);
			saveMessage(request,getText("bOrder.deleted",new Object[]{bOrder.getId()}));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 查看实体对象详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView viewDetail(HttpServletRequest request,	HttpServletResponse response){
		String id=request.getParameter("id");
		BOrder bOrder=null;
		if(StringUtils.isNotEmpty(id)){
			bOrder=bOrderManager.get(new Long(id));
			
			UserAccount account=RequestContext.getCurrentUser();
			if(account!=null){
				String action="查看订单"+bOrder.getOrderNo();
				orderLogManager.saveLog(action, "1",bOrder.getId().toString());
			}
		}
		return new ModelAndView("member/order/orderDetail","bOrder",bOrder);
	}
	
	/**
	 * 取消订单
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView cancelOrder(HttpServletRequest request,	HttpServletResponse response){
		String id=request.getParameter("id");
		BOrder bOrder=null;
		if(StringUtils.isNotEmpty(id)){
			bOrder=bOrderManager.get(new Long(id));
			
			Boolean flag=true;
			if(bOrder.getStatus()==0){
				String user = ConfigUtil.getInstance().getProperty("abe.userId");
				String password = ConfigUtil.getInstance().getProperty("abe.password");
				String server = ConfigUtil.getInstance().getProperty("abe.server");
				String port = ConfigUtil.getInstance().getProperty("abe.port");
				String maxPage = ConfigUtil.getInstance().getProperty("abe.maxPage");

				String identity = getText("abe.identity", new Object[] { user,
						password, server, port, maxPage });
				flag=bOrderManager.xePnr(bOrder.getPnr(), identity);
			}
			if(flag){
				bOrder.setStatus(new Long(12));
				bOrderManager.save(bOrder);
				String action="取消订单"+bOrder.getOrderNo();
				orderLogManager.saveLog(action, "1",bOrder.getId().toString());
			}
		}
		return new ModelAndView(getSuccessView());
	}
	
	
	public ModelAndView updateOrder(HttpServletRequest request,	HttpServletResponse response){
		String id=request.getParameter("id");
		String status=request.getParameter("status");
		BOrder bOrder=null;
		if(StringUtils.isNotEmpty(id)&&StringUtils.isNotBlank(status)){
			bOrder=bOrderManager.get(new Long(id));
			String oldText=getText("bOrder.status"+bOrder.getStatus());
			String newText=getText("bOrder.status"+status);
			String action="订单"+bOrder.getOrderNo()+","+oldText+"修改为"+newText;
			bOrder.setStatus(new Long(status));
			bOrderManager.save(bOrder);
			orderLogManager.saveLog(action, "2",bOrder.getId().toString());
		}
		return new ModelAndView(getSuccessView());
	}
	
}
